#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "../inc/student.h"
#include "../inc/admin.h"
#include "../inc/general.h"

char Login_Username[10];
char *glpassword = "wyf";

// 打印登录菜单函数
void printf_login_menu()
{
    printf("             欢迎来到学生管理系统           \n");
    print_Lines();
    printf("| 1、登录\t\t\t             |\n");
    printf("| 2、注册\t\t\t             |\n");
    printf("| 0、退出\t\t\t             |\n");
    print_Lines();
    printf("请输入相应的序号选择!          \n");
}

// 执行登录菜单
int menuOperate_login(int menu, Admin_Node_p admin_head)
{
    while (getchar() != '\n')
        ;
    system("cls"); // 清空控制台内容
    switch (menu)
    {
    case LOGIN:
        return login(admin_head);
        break;
    case ENROLL:
        enroll(admin_head);
        break;
    case EXIT_SYSTEM:
        printf("退出成功!");
        exit(0);
    default:
        break;
    }
    return 0;
}

// 获取权限
char *getPrivilege(int privilege)
{
    if (privilege == 0)
    {
        return "普通用户";
    }
    else if (privilege == 1)
    {
        return "管理员";
    }
}

// 注册
void enroll(Admin_Node_p admin_head)
{
    printf("                    注册                  \n");
    print_Lines();
    insertTail_adminNode(admin_head);
    print_Lines();
    printf("注册成功!\n");
    // Sleep(1000);
}

// 登录
int login(Admin_Node_p admin_head)
{
    printf("                     登录                  \n");
    print_Lines();
    Admin_Node_p p = admin_head;
    char username[10];
    char password[10];
    printf("请输入账号:");
    scanf("%9s", username);
    printf("请输入密码:");
    scanf("%9s", password);
    int flag = verify(admin_head, username, password);
    print_Lines();
    if (flag == 10 || flag == 11)
    {
        strcpy(Login_Username, username); // 登录成功，保存用户名
        printf("登录成功!正在进入系统....\n");
        Sleep(1000);
        return flag;
    }
    else if (flag == -1)
    {
        printf("账号不存在!\n");
        // Sleep(1000);
    }
    else if (flag == -2)
    {
        printf("密码错误!\n");
        // Sleep(1000);
    }
    return 0;
}

// 验证账号是否存在以及密码是否正确
int verify(Admin_Node_p admin_head, char *username, char *password)
{
    Admin_Node_p p = admin_head->next;
    while (p != admin_head)
    {
        if (strcmp(p->data->username, username) == 0)
        {
            if (strcmp(p->data->password, password) == 0)
            {

                return 10 + p->data->privilege; // 返回0表示账号和密码都正确
            }
            else
            {
                return -2; // 返回1表示密码错误
            }
        }
        else
        {
            p = p->next;
        }
    }
    return -1; // 返回-1表示账号不存在
}

// 显示所有管理员信息
void show_allAdmin(Admin_Node_p admin_head)
{
    const int pageSize = 5; // 每页显示5条记录
    int total = getAdminCount(admin_head);
    int totalPages = (total + pageSize - 1) / pageSize; // 计算总页数
    int currPage = 1;
    Admin_Node_p currNode = admin_head->next;

    do
    {
        printf("          全部账号信息(第%d页/共%d页)\n", currPage, totalPages);
        print_Lines();
        printf("共有%d 个账号信息,信息如下:\n", total);
        print_Lines2();
        printf("用户名\t\t权限\n");
        print_Lines2();
        int count = 0;
        Admin_Node_p pageStart = currNode; // 保存当前页起始节点
        while (currNode != admin_head && count < pageSize)
        {
            printf("%s\t\t%s\n", currNode->data->username, getPrivilege(currNode->data->privilege));
            count++;
            currNode = currNode->next;
        }
        print_Lines();
        printf("指令: (A)上一页 (D)下一页 (Q)退出: ");

        // 翻页
        while (1)
        {
            char input;
            scanf(" %c", &input);
            while (getchar() != '\n')
                ; // 清空输入缓冲区
            if (input == 'a' || input == 'A')
            {
                if (currPage > 1)
                {
                    Admin_Node_p newStart = pageStart;
                    for (int i = 0; i < pageSize && newStart->prev != admin_head; i++)
                    {
                        newStart = newStart->prev;
                    }
                    if (newStart != pageStart)
                    { // 确认指针已移动
                        currNode = newStart;
                        currPage--;
                        clearScreen();
                        break;
                    }
                }
                else
                {
                    printf("已经是第一页了,请按D移到下一页:");
                }
            }
            else if (input == 'd' || input == 'D')
            {
                if (currPage < totalPages)
                {
                    if (currNode != admin_head)
                    { // 确保没有越界
                        currPage++;
                        clearScreen();
                        break;
                    }
                }
                else
                {
                    printf("已经是最后一页了,请按A移到上一页:");
                }
            }
            else if (input == 'q' || input == 'Q')
            {
                return;
            }
            else
            {
                printf("输入错误,请重新输入:");
            }
        }

    } while (1);
}

// 权限管理
void give_privilege(Admin_Node_p admin_head)
{
    printf("                账号权限管理\n");
    print_Lines();
    printf("请输入要授权的账号:");
    char username[10];
    scanf("%9s", username);
    if (strcmp(username, Login_Username) == 0)
    {
        printf("不能修改自己的权限!\n");
        if (return_function() == 1)
        {
            give_privilege(admin_head);
        }
        return;
    }
    Admin_Node_p currNode = find_adminNode(admin_head, getAdminIndex_ByUserName(admin_head, username));
    if (currNode == NULL)
    {
        printf("未找到该账号!\n");
        if (return_function() == 1)
        {
            give_privilege(admin_head);
        }
        return;
    }
    else
    {
        clearScreen();
        authorization(currNode, username);
    }
}

// 授权
void authorization(Admin_Node_p currNode, char *username)
{
    printf("                账号权限管理\n");
    print_Lines();
    printf("请输入要授权的账号:%s\n", username);
    printf("请输入要授权的权限(0:普通用户,1:管理员):");
    int privilege;
    scanf("%d", &privilege);
    if (privilege == 0 || privilege == 1)
    {
        if (privilege == 1 && currNode->data->privilege == 0)
        {
            printf("请输入管理密码:");
            char password[10];
            scanf("%9s", password);
            if (strcmp(password, glpassword) == 0)
            {
                currNode->data->privilege = privilege;
                printf("用户 %s 已被授权为 %s!\n", username, getPrivilege(currNode->data->privilege));
                return_Main();
            }
            else
            {
                printf("密码错误!\n");
                if (return_function() == 1)
                {
                    authorization(currNode, username);
                }
            }
        }
        else if (privilege == 0 && currNode->data->privilege == 1)
        {
            printf("请输入管理密码:");
            char password[10];
            scanf("%9s", password);
            if (strcmp(password, glpassword) == 0)
            {
                currNode->data->privilege = privilege;
                printf("用户 %s 已被降级为 %s!\n", username, getPrivilege(currNode->data->privilege));
                return_Main();
            }
            else
            {
                printf("密码错误!\n");
                if (return_function() == 1)
                {
                    authorization(currNode, username);
                }
            }
        }
        else
        {
            printf("用户 %s 已经是 %s 了!\n", username, getPrivilege(privilege));
            if (return_function() == 1)
            {
                authorization(currNode, username);
            }
        }
    }
    else
    {
        printf("输入错误!\n");
        if (return_function() == 1)
        {
            authorization(currNode, username);
        }
    }
}